package niit.train2017_3_1;

import android.content.Intent;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class DynamicActivity extends AppCompatActivity{

    SensorManager manager;
    Sensor sensor;
    boolean isShacke = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dynamic);
    }


    @Override
    protected void onResume() {
        super.onResume();
        manager.registerListener(listener,sensor,SensorManager.SENSOR_DELAY_FASTEST);
        isShacke = false;
    }


    @Override
    protected void onPause() {
        super.onPause();
        if (manager!=null);
        manager.unregisterListener(listener);
    }

    private SensorEventListener listener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent sensorEvent) {
            int sensorType = sensorEvent.sensor.getType();

            if (sensorType == sensor.TYPE_ACCELEROMETER){
                float[] values = sensorEvent.values;
                float x = values[0];
                float y = values[1];
                float z = values[2];
                int value = 15;
                if (x >= value || x <= -value || y >= value || y <= -value || z >= value || z <= -value){
                    if (!isShacke){
                        isShacke = true;
                        Intent intent = new Intent(DynamicActivity.this,QrcodeActivity.class);
                        startActivity(intent);
                    }
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int i) {

        }
    };
}
